/*
 * JPanelWizardSelectAlign.java
 *
 * Created on 04-sep-2011, 20:25:31
 */
package com.ufps.cuatro.vistas.representacion.wizard.clase;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JList;
import javax.swing.ListSelectionModel;

import com.ufps.cuatro.modelo.representacion.ObjetoTipoDatoReflexion;
import com.ufps.cuatro.modelo.representacion.UtilRepresentacion;
import com.ufps.cuatro.util.TipoCaso;
import com.ufps.cuatro.util.Utileria;
import com.ufps.cuatro.util.model.MyCheckItemPropertiesClass;
import com.ufps.cuatro.util.render.MyListRendererJCheckBox;

/**
 *
 * @author Carlos A. Torres C. 
 */
public class JPanelWizardSelectPropierties extends javax.swing.JPanel {
    private static final long serialVersionUID = -1906702795411867723L;
    
    private ObjetoTipoDatoReflexion objTipoDato;   
    private Object objParent;
    private ArrayList<MyCheckItemPropertiesClass> objMetodos = null;
    private boolean SETGET; 

    /** Creates new form JPanelWizardSelectAlign */
    public JPanelWizardSelectPropierties() {
        initComponents();   
//        WizardPanelDescriptor obj = UtilWizard.objWizard.getModel().getPanelDescriptor(JPanelWizardSelectClassDescriptor.IDENTIFIER);
//        JPanelWizardSelectClass panel = (JPanelWizardSelectClass) obj.getPanelComponent();
//        this.objTipoDato = panel.getObjTipoDato();
//        this.cargarPropiedades();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane2 = new javax.swing.JScrollPane();
        jListMetodos = new javax.swing.JList();
        jSeparator1 = new javax.swing.JSeparator();

        setName("Form"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jListMetodos.setName("jListMetodos"); // NOI18N
        jScrollPane2.setViewportView(jListMetodos);

        jSeparator1.setName("jSeparator1"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 461, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(22, 22, 22)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE))
                    .addGap(23, 23, 23)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 256, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 145, Short.MAX_VALUE)
                    .addGap(58, 58, 58)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(32, 32, 32)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void loadRendererJListMethod() {
        JList objLista = new JList(objMetodos.toArray());
        this.jListMetodos.setModel(objLista.getModel());
        this.jListMetodos.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
        MyListRendererJCheckBox objRender = new MyListRendererJCheckBox();
        this.jListMetodos.setCellRenderer(objRender);        
    }
    
    private boolean cargarPropiedades(){
        try{
            Class<?> objClass = null;
            String strClassPath = this.objTipoDato.getStrClassPath();
            //Se comprueba que objeto la clase este registrada
            if(this.objTipoDato.getObjClass() != null){
                objClass = this.objTipoDato.getObjClass();
            }
            else{                
                objClass = Class.forName(strClassPath);
            }
            //se extraen las propiedades;            
            Method[] objTempMetodos = objClass.getMethods();              
            List<Method> ListMethods = Arrays.asList(objTempMetodos);
            this.objMetodos     = new ArrayList<MyCheckItemPropertiesClass>();                
            
            for(Method objMethod : ListMethods){
                if(SETGET){
                    if((UtilRepresentacion.isGetter(objMethod) || UtilRepresentacion.isIs(objMethod)) && !UtilRepresentacion.isMetodoExcluido(objMethod)){
                        MyCheckItemPropertiesClass objItem = new MyCheckItemPropertiesClass(objMethod);
                        if(objTipoDato.getObjCaso().geteCaso() == TipoCaso.eJDKNoRegistrado){
                            Class<?> objClassTipoDato = objMethod.getReturnType();
                            if(UtilRepresentacion.isExisteTipoDato(objClassTipoDato)){
                                this.objMetodos.add(objItem);
                            }
                        }
                        else{
                            this.objMetodos.add(objItem);
                        }
                    }
                    else if(UtilRepresentacion.isSetter(objMethod) && !UtilRepresentacion.isMetodoExcluido(objMethod)){
                        MyCheckItemPropertiesClass objItem = new MyCheckItemPropertiesClass(objMethod);
                        if(objTipoDato.getObjCaso().geteCaso() == TipoCaso.eJDKNoRegistrado){
                            Class<?> objClassTipoDato = objMethod.getParameterTypes()[0];
                            if(UtilRepresentacion.isExisteTipoDato(objClassTipoDato)){
                                this.objMetodos.add(objItem);
                            }
                        }
                        else{
                            this.objMetodos.add(objItem);
                        }
                    }
                }
                else{
                    if(!UtilRepresentacion.isGetter(objMethod) && 
                            !UtilRepresentacion.isIs(objMethod) && 
                            !UtilRepresentacion.isSetter(objMethod) &&
                            !UtilRepresentacion.isMetodoExcluido(objMethod)){
                        MyCheckItemPropertiesClass objItem = new MyCheckItemPropertiesClass(objMethod);
                        if(objTipoDato.getObjCaso().geteCaso() == TipoCaso.eJDKNoRegistrado){
                            Class<?> objClassTipoDato = objMethod.getReturnType();
                            if(UtilRepresentacion.isExisteTipoDato(objClassTipoDato)){
                                this.objMetodos.add(objItem);
                            }
                        }
                        else{
                            this.objMetodos.add(objItem);
                        }
                    }                                        
                }
            }
            
            if(this.objMetodos.isEmpty())return false;
            loadRendererJListMethod();
            return true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(JPanelWizardSelectPropierties.class.getName()).log(Level.SEVERE, null, ex);
        }catch(java.lang.NullPointerException ex){            
            Logger.getLogger(JPanelWizardSelectPropierties.class.getName()).log(Level.SEVERE, null, ex);
        }catch(java.lang.IndexOutOfBoundsException ex){            
            Logger.getLogger(JPanelWizardSelectPropierties.class.getName()).log(Level.SEVERE, null, ex);
        }catch(java.lang.ArrayStoreException ex){            
            Logger.getLogger(JPanelWizardSelectPropierties.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    private void reiniciarCargarPropiedadesClase() {        
        this.objMetodos.clear();        
        this.loadRendererJListMethod();
    }
    
    private void seleccionarMetodos(int modo, boolean estado){
        switch(modo){
            case Utileria.GET://get
                for(MyCheckItemPropertiesClass objItem : this.objMetodos){
                    Method objMetodo = objItem.getObjMethod();
                    if(UtilRepresentacion.isGetter(objMetodo) || UtilRepresentacion.isIs(objMetodo)){
                        objItem.setSelected(estado);
                    }                
                }
                break;
            case Utileria.SET://set
                for(MyCheckItemPropertiesClass objItem : this.objMetodos){
                    Method objMetodo = objItem.getObjMethod();
                    if(UtilRepresentacion.isSetter(objMetodo)){
                        objItem.setSelected(estado);
                    }
                }
                break;
            case Utileria.CLS://borrar
                for(MyCheckItemPropertiesClass objItem : this.objMetodos){                    
                    objItem.setSelected(estado);                    
                }
                break;
        }
        loadRendererJListMethod();        
    }     
        
        
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList jListMetodos;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration//GEN-END:variables
}
